package com.lc.tgxm.conn;

import com.lc.tgxm.model.DTO;
import com.zcx.helper.adapter.AppRecyclerAdapter;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.AsyPostForm;
import com.zcx.helper.http.note.HttpSecret;
import com.zcx.helper.http.note.HttpServer;
import com.zcx.helper.secret.SecretRSA;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * BaseAsyPost
 * Created by Administrator on 2016/2/17.
 */
@HttpSecret(key = Conn.HTTPSECRET)
@HttpServer(Conn.SERVICE)
public class BaseAsyPost<T> extends AsyPostForm<T> {

    public BaseAsyPost(AsyCallBack<T> asyCallBack) {
        super(asyCallBack);
        if (Conn.ENCRYPTABLE) {
            SECRET_REQUEST = SECRET_RESPONSE = new SecretRSA("rsa_public_key.pem", "pkcs8_rsa_private_key.pem");
        }
    }

    @Override
    protected T parser(JSONObject object) {
        TOAST = object.optString("message");
        int code = object.optInt("code");
        return code == Conn.CODE_SUCCESS ? successParser(object) : failParser(object);
    }

    protected T successParser(JSONObject object) {
        return null;
    }

    protected T failParser(JSONObject object) {
        return null;
    }

    protected void initDTOData(JSONObject object, DTO dto) {
        JSONObject jsonObject = object.optJSONObject("data");
        if (jsonObject != null) {
            dto.totalPage = jsonObject.optInt("last_page");
            JSONArray jsonArray = jsonObject.optJSONArray("data");
            if (jsonArray != null) {
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jobj = jsonArray.optJSONObject(i);
                    if (jobj != null) {
                        // noinspection unchecked
                        dto.arrayList.add(jsonData(jobj));
                    }
                }
            }
        }
    }

    protected AppRecyclerAdapter.Item jsonData(JSONObject object) {
        return null;
    }

}